﻿Ext.define('HIT.view.SetRuleOrderPanel', {
    extend : 'Ext.form.Panel',
    alias : 'widget.setregularorder',
    height: 380,
    width: 510,
    bodyPadding: 10,

    autoScroll: true,
    collapsible: false,
    overlapHeader: false,
    
    initComponent: function() {
        var me = this;
		var myId = 0;
		
		function ruleFieldset(param) {
			myId++;
			var newField = new Ext.form.Panel({
				xtype : 'panel',
				margin : '10 0 10',
				id : 'order_panel' + myId,
				layout : {
					type : 'absolute'
				},
				border : false,
				height : 24,
				items : [{
					xtype : 'label',
					id : 'order_label' + myId,
					x : 70,
					text : myId + ':'
				}, {
					xtype : 'combobox',
					x : 110,
					width : 100,
					id : 'order_combo' + myId,
					valueField : 'value',
					displayField : 'type',
					msgTarget : 'side',
					blankText : Share.T('规则不可为空'),
					allowBlank : false,
					queryDelay : 20,
					editable : false,
					store : new Ext.data.JsonStore({
						proxy : {
							type : 'ajax',
							url : ctx + '/getRuleSort',
							params : {},
							reader : {
								type : 'json',
								totalProperty : '',
								root : ''
							}
						},
						fields : ['type','value'],
						autoLoad : true,
						listeners : {
	    					load : function(store,record) {//过滤规则下拉框
	    						store.filterBy(function(record) {
	    							if (param == 'special') {
	    								return record.get('value') < 20 && record.get('value') >= 10;
	    							}
	    							if (param == 'replace') {
	    								return record.get('value') < 30 && record.get('value') >= 20;
	    							}
	    							if (param == 'userDefined') {
	    								return record.get('value') < 40 && record.get('value') >= 30;
	    							}
	    						});
	    					}
						}
					})
				} ]
			});
			Ext.getCmp(param).insert(myId,newField);//在insert的位置插入
		};
		
		Share.AjaxRequest({
		    url : ctx + "/getRuleSort",
		    callback : function(json) {
		    	var specialCount = 0;
		    	var replaceCount = 0;
		    	var userDefinedCount = 0;
		        for(var i = 0; i<json.length; i++){
		        	if(json[i].value >= 10 && json[i].value < 20){
		        		specialCount++;
		        		ruleFieldset('special');
		        		Ext.getCmp('order_combo' + myId).setValue(json[i].value);
		        	}
		        }
		        for(var i=0; i < json.length; i++){
		        	if(json[i].value >= 20&&json[i].value < 30){
		        		replaceCount++;
		        		ruleFieldset('replace');
		        		Ext.getCmp('order_combo' + myId).setValue(json[i].value);
		        	}
		        }
		        for(var i=0; i < json.length; i++){
		        	if(json[i].value >= 30 && json[i].value < 40){
		        		userDefinedCount++;
		        		ruleFieldset('userDefined');
		        		Ext.getCmp('order_combo' + myId).setValue(json[i].value);
		        	}		        	
		        }
		        if(specialCount == 0){
		        	Ext.getCmp('special').hide();
		        }
		        if(replaceCount == 0){
		        	Ext.getCmp('replace').hide();
		        }  
		        if(userDefinedCount == 0){
		        	Ext.getCmp('userDefined').hide();
		        }
		        if(specialCount == 0 && replaceCount == 0 && userDefinedCount == 0){
		        	Ext.MessageBox.alert(Share.T('提示'),Share.T('没有规则被选中'));
		        }
		    }
		});
		
        Ext.applyIf(me, {
            items: [ {
					xtype : 'fieldset',
					title : Share.T('特殊规则'),
					id : 'special',
					border : 'fasle',
					width : 480,
					items : []
				}, {
					 xtype : 'fieldset',
					 title : Share.T('替换规则'),
					 id : 'replace',
					 border : 'fasle',
	                 width : 480,
	                 items : []
				}, {
					xtype : 'fieldset',
					title : Share.T('自定义规则'),
					id : 'userDefined',
					border : 'fasle',
	                 width : 480,
					items : []
				} ]
        	}
		);
		me.callParent(arguments);
	}
});